package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.Redisconstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import com.itheima.health.util.AliOssUtil;
import com.itheima.health.util.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author zhangmeng
 * @description 套餐控制器
 * @date 2019/9/26
 **/
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetMealController {
//    @Autowired
//    private QiniuUtils qiniuUtils;

    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private SetMealService setMealService;

    @Autowired
    private RedisTemplate redisTemplate;
//    /**
//     * 上传图片
//     * @param multipartFile
//     * @return
//     */
//    @PostMapping("/upload")
//    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) {
//        log.info("文件上传，name:{},size:{}", multipartFile.getOriginalFilename(), multipartFile.getSize());
//        //原始文件名
//        String originalFileName = multipartFile.getOriginalFilename();
//        //使用UUID构造不重复的文件名
//        String fileName = UUID.randomUUID().toString().replace("-", "") + "_" + originalFileName;
//
//        //获取输入流并上传
//        try (InputStream is = multipartFile.getInputStream()) {
//            qiniuUtils.upload2Qiniu(is, fileName);
//        } catch (IOException e) {
//            log.error("", e);
//            return new Result(false, MessageConst.PIC_UPLOAD_FAIL);
//        }
//
//        //构造返回值
//        String pic = qiniuUtils.getUrlPrefix() + fileName;
//        return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS, pic);
//    }

    /**
     * 文件上传
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        //日志记录
        log.info("文件上传：{}",imgFile);
        try {
            //原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            //截取原始文件后缀名 .png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件的名称
            String objectName = UUID.randomUUID().toString() + extension;
            //文件路径
            String filePath = aliOssUtil.upload(imgFile.getBytes(),objectName);

            String key = Redisconstant.SETMEAL_PIC_RESOURCES;
            redisTemplate.opsForSet().add(key,filePath);

            return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS,filePath);
        } catch (IOException exception) {
            log.info("文件上传失败:{}",exception);
        }
        return new Result(false,MessageConst.PIC_UPLOAD_FAIL);
    }

    /**
     * 添加套餐
     * @param setmeal   套餐基本信息
     * @param checkgroupIds 检查组ID列表
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal , Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
//        if(!StringUtils.isEmpty(setmeal.getImg())) {
//            String img =setmeal.getImg().replace(aliOssUtil.getUrlPrefix(),"");
//            setmeal.setImg(img);
//        }
        //RPC调用添加数据
        setMealService.add(setmeal,checkgroupIds);
        return new Result(true,MessageConst.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        //调用DAO层查询并返回
        PageResult pageResult = setMealService.findPage(queryPageBean);
        //构造返回结果并返回
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,pageResult);
    }

    /**
     * 根据套餐id查询数据
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("[套餐-根据套餐id查询数据]id:{}",id);
        Setmeal setmeal = setMealService.findById(id);
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    /**
     * 编辑套餐
     * @param setmeal
     * @param checkGroupIds
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkGroupIds){
        log.info("[套餐-编辑]data:{},ids:{}",setmeal,checkGroupIds);
        setMealService.edit(setmeal,checkGroupIds);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    /**
     * 根据套餐id删除数据
     * @param id
     * @return
     */
    @PostMapping("/deleteSetmealById")
    public Result deleteById(Integer id){
        log.info("[根据套餐id删除数据]id:{}",id);

        try {
            setMealService.deleteById(id);
            return new Result(true,MessageConst.ACTION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }
}
